/*
 * Copyright 2012 Faculty of Informatics - Masaryk University.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.pa165.creatures.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * This entity represents a region in our world where some {@link Creature}s
 * are present.
 *
 * @author smikloso
 */
@Entity
@Table(uniqueConstraints = {
    @UniqueConstraint(columnNames = "NAME")
})
@XmlRootElement
public class Region implements Serializable {

    private static final long serialVersionUID = 295494090283310611L;

    /**
     * Id of the region where creatures live.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    @Column(name = "ID", nullable = false)
    private Long id;

    /**
     * The name of the region which has to be set in order to persist it.
     */
    @Column(name = "NAME", length = 255, nullable = false)
    private String name;

    /**
     * Description of the area.
     */
    @Column(name = "DESCRIPTION", length = 512)
    private String description;

    /**
     * Area of the region (e.g. in square kilometers).
     */
    @Column(name = "AREA")
    private Long area;

    /**
     * Creatures which lives in a region.
     */
    @OneToMany(mappedBy = "region", cascade = {CascadeType.REMOVE})
    @XmlTransient
    private List<Creature> creatures = new ArrayList<Creature>();

    public Region() {
    }

    /**
     *
     * @param name name of a region
     */
    public Region(String name) {
        this.name = name;
    }

    /**
     *
     * @param name name of a region
     * @param description description of a region
     * @param area area of a region in square kilometers
     */
    public Region(String name, String description, Long area) {
        this.name = name;
        this.description = description;
        this.area = area;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Long getArea() {
        return area;
    }

    public void setArea(Long area) {
        this.area = area;
    }

    /**
     * Gets all creatures which live in the region. The
     * returned list is a defensive copy.
     *
     * @return creatures which live in the region
     */
    public List<Creature> getCreatures() {
        return new ArrayList<Creature>(creatures);
    }

    /**
     * This method remove a {@code creature} from the {@code region},
     * if a {@code creature} lives in it, preserving the relationship consistency.
     *
     * @param creature creature to remove from the region
     */
    public void removeCreature(Creature creature) {
        if (creature != null) {
            if (!creatures.contains(creature)) {
                return;
            }
            creatures.remove(creature);
            creature.setRegion(null);
        }
    }

    /**
     * This method adds a {@code creature} to the region, preserving the
     * relationship consistency. {@code this} region is set as the
     * region a {@code creature} lives in.
     *
     * @param creature creature to add to the region
     */
    public void addCreature(Creature creature) {
        if (creature != null) {
            if (creatures.contains(creature)) {
                return;
            }
            creatures.add(creature);
            creature.setRegion(this);
        }
    }

    @Override
    public String toString() {
        return "Region{" + "id=" + id + ", name=" + name + '}';
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 29 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Region other = (Region) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

}
